package br.com.array.android.ma.service;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * Created by leonardo.neves on 06/08/2014.
 */
public class HttpRequest {

    private QueryParam queryParam;

    private static class Parameters {
        private static final String URI_BASE = "";
        private static final String TITULO = "";
        private static final String DESCRICAO = "";
        private static final String DATA = "";
    }

    public HttpRequest(QueryParam queryParam) {
        this.queryParam = queryParam;
    }

    private String addURLParam(){
        String url = "";

        if (!queryParam.getTitulo().isEmpty()){
            url = formatParam(Parameters.TITULO, queryParam.getTitulo());
        }
        if (!queryParam.getDescricao().isEmpty()){
            url += formatParam(Parameters.DESCRICAO, queryParam.getDescricao());
        }
        if (!queryParam.getData().isEmpty()){
            url += formatParam(Parameters.DATA, queryParam.getData());
        }
        return url;
    }

    private String formatParam(String parameter, String value){
        return parameter + "=" + value + "&";
    }

    public String getUrlResponse() throws NotificacaoException, IOException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(Parameters.URI_BASE + addURLParam());
        HttpResponse httpResponse = httpClient.execute(httpGet);

        HttpEntity httpEntity = httpResponse.getEntity();
        return EntityUtils.toString(httpEntity);
    }
}
